<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>用户管理</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" href="../../layui/css/layui.css" media="all">
  <link rel="stylesheet" href="../../css/admin.css" media="all">
</head>
<body>

<div class="layui-fluid">
  <div class="layui-card">
    <div class="layui-form layui-card-header layuiadmin-card-header-auto">
      <div class="layui-form-item">
        <div class="layui-inline">
          <label class="layui-form-label">用户名称</label>
          <div class="layui-input-block">
            <input type="text" name="username" placeholder="请输入" autocomplete="off" class="layui-input">
          </div>
        </div>
        <div class="layui-inline">
          <label class="layui-form-label">使用状态</label>
          <div class="layui-input-block">
            <select name="status">
              <option value="0">冻结</option>
              <option value="1">正常</option>
            </select>
          </div>
        </div>
        <div class="layui-inline">
          <button class="layui-btn layuiadmin-btn-useradmin" lay-submit lay-filter="LAY-user-front-search">
            <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
          </button>
        </div>
      </div>
    </div>

    <div class="layui-card-body">
      <div style="padding-bottom: 10px;">
        <button class="layui-btn layuiadmin-btn-useradmin" data-type="batchdel">删除</button>
        <button class="layui-btn layuiadmin-btn-useradmin" data-type="add">添加</button>
      </div>
      <!--动态数据表格-->
      <table id="LAY-user-manage" lay-filter="LAY-user-manage"></table>
      <!--&lt;!&ndash;头像显示&ndash;&gt;-->
      <!--<script type="text/html" id="imgTpl">-->
        <!--<img style="display: inline-block; width: 50%; height: 100%;" src= {{ d.avatar }} onclick="showBigImage(this);">-->
      <!--</script>-->
      <!--状态判断-->
      <script type="text/html" id="statusTpl">
        {{#  if(d.status == 1){ }}
        <button class="layui-btn layui-btn-xs">正常</button>
        {{#  } else if(d.status == 2) { }}
        <button class="layui-btn layui-btn-primary layui-btn-xs">冻结</button>
        {{#  } }}
      </script>

      <script type="text/html" id="table-useradmin-webuser">
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="lock"><i class="layui-icon layui-icon-set"></i>冻结</a>
        <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>修改</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
      </script>
    </div>
  </div>
</div>

<script src="../../js/jquery-3.4.1.min.js"></script>
<script src="../../layui/layui.js"></script>
<script type="text/javascript">
    layui.use(['layer','table','form','laydate'], function(){
        var $ = layui.$
            ,form = layui.form
            ,layer = layui.layer
            ,table = layui.table;

        //加载table
        table.render({
            elem: '#LAY-user-manage'
            ,url:'/findUsers'
            ,cellMinWidth: 80 //全局定义常规单元格的最小宽度
            ,title: '用户数据表'
            ,cols: [[
                {type: 'checkbox', fixed: 'left'}
                ,{field:'id', title:'ID'}
                ,{field:'login_account', title:'登录账号'}
                ,{field:'user_name', title:'用户名称'}
                ,{field:'role_id', title:'角色'}
                ,{field:'email', title:'邮箱地址',templet: function(res){
                    return '<em>'+ res.email +'</em>'
                }}
                ,{field:'entry_date', title:'入职日期'}
                ,{field:'resign_date', title:'离职日期'}
                ,{field:'resign_date', title:'最后登录时间'}
                ,{field:'status', title:'状态', templet:'#statusTpl'}
                ,{fixed: 'right', title:'操作', toolbar: '#table-useradmin-webuser',width:250}
            ]]
            ,page: true
        });


        //监听搜索
        form.on('submit(LAY-user-front-search)', function(data){
            var field = data.field;

            //执行重载
            table.reload('LAY-user-manage', {
                where: field
            });
        });

        //事件
        var active = {
            batchdel: function(){
                var checkStatus = table.checkStatus('LAY-user-manage')
                    ,checkData = checkStatus.data; //得到选中的数据
                if(checkData.length === 0){
                    return layer.msg('请选择数据');
                }
                //删除确定信息
                layer.confirm('确定删除吗？', function(index) {
                     //定义空数组
                     var ids = new Array();
                     $.each(checkData,function (index,item) {
                         //每循环一次，将id取出，塞给数组。
                         ids.push(item.id)
                     })
                    //通过Ajax访问后台实现删除功能
                    $.ajax({
                        url:"/batchUserdel",
                        type:"post",
                        data:{ids:ids}, //直接传数组
                        traditional:true, //用传统的方式来序列化数据
                        dataType:"json",
                        success:function(res){
                             if(res.code == 200){
                                 layer.msg(res.msg);
                                 //执行重载
                                 table.reload('LAY-user-manage', {
                                     page: {
                                         curr: 1 //重新从第 1 页开始
                                     }
                                     ,where: {
                                         key: {
                                             id: new Date().getTime()
                                         }
                                     }
                                 });
                             }
                             if(res.code == 500){
                                layer.msg(res.msg);
                            }
                        }
                    })
                    //表格重载
//                    table.reload('LAY-user-manage');
                });

            }
            ,add: function(){
                layer.open({
                    type: 2
                    ,title: '添加用户'
                    ,content: 'user/userform.html'
                    ,maxmin: true
                    ,area: ['460px', '570px']
                    ,btn: ['确定', '取消']
                    ,yes: function(index, layero){
                        var iframeWindow = window['layui-layer-iframe'+ index]
                            ,submitID = 'LAY-user-front-submit'
                            ,submit = layero.find('iframe').contents().find('#'+ submitID);
                        //监听提交
                        iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                            var field = data.field; //获取提交的字段
                            console.log(field)
                            //提交 Ajax 成功后，静态更新表格中的数据
                            $.ajax({
                                url:"/addUser",
                                type:"post",
                                data:field,
                                dataType:"json",
                                success:function (res) {
                                    if(res.code == 200){
                                        layer.msg(res.msg);
                                        //执行重载
                                        table.reload('LAY-user-manage', {
                                           where: {
                                                key: {
                                                    id: new Date().getTime()
                                                }
                                            }
                                        });
                                    }
                                }
                            });
                            table.reload('LAY-user-front-submit'); //数据刷新
                            layer.close(index); //关闭弹层
                        });

                        submit.trigger('click');
                    }
                });
            }
        };

        //监听行工具事件
        table.on('tool(LAY-user-manage)', function(obj){
            var data = obj.data;
//            console.log(data)
            if(obj.event === 'lock'){
                 alert("冻结")
            } else if(obj.event === 'edit'){
                layer.open({
                    type: 2
                    ,title: '修改用户'
                    ,content: 'user/edituserform.html'
                    ,maxmin: true
                    ,area: ['460px', '570px']
                    ,btn: ['确定', '取消']
                    ,success: function(layero, index){//在页面加载成功之前将数据回显
                         var body = layer.getChildFrame('body', index);
                        var iframeWin = layero.find('iframe')[0].contentWindow; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                        console.log(body.html()) //得到iframe页的body内容
                        body.find('input[name=id]').val(data.id);
                        body.find('input[name=login_account]').val(data.login_account);
                        body.find('input[name=user_name]').val(data.user_name);
                        body.find('select option[value='+data.role_id+']').attr("selected","selected");
                        body.find('input[name=email]').val(data.email);
                        body.find('input[name=entry_date]').val(data.entry_date);
                        body.find('input[name=resign_date]').val(data.resign_date);
                        body.find('#status input[value='+data.status+']').attr("checked","checked");
                        //重载
                        iframeWin.layui.form.render("select");
                        iframeWin.layui.form.render("radio");


                    }
                    ,yes: function(index, layero){
                        var iframeWindow = window['layui-layer-iframe'+ index]
                            ,submitID = 'LAY-user-front-submit'
                            ,submit = layero.find('iframe').contents().find('#'+ submitID);
                        //监听提交
                        iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                            var field = data.field; //获取提交的字段
                            console.log(field)
                            //提交 Ajax 成功后，静态更新表格中的数据
                            $.ajax({
                                url:"/addUser",
                                type:"post",
                                data:field,
                                dataType:"json",
                                success:function (res) {
                                    if(res.code == 200){
                                        layer.msg(res.msg);
                                        //执行重载
                                        table.reload('LAY-user-manage', {
                                            where: {
                                                key: {
                                                    id: new Date().getTime()
                                                }
                                            }
                                        });
                                    }
                                }
                            });
                            table.reload('LAY-user-front-submit'); //数据刷新
                            layer.close(index); //关闭弹层
                        });

                        submit.trigger('click');
                    }
                });



            } else if(obj.event === 'del'){
                alert("删除")
            }
        });

        $('.layui-btn.layuiadmin-btn-useradmin').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

    });
</script>

</body>
</html>
